const { default: axios } = require('axios');

const router = require('express').Router()

/**
 * @api {get} /search 获取获取搜索结果
 * @apiName 获取搜索结果
 * @apiGroup search(搜索相关)
 * @apiDescription 获取搜索结果
 * @apiVersion  1.0.0
 * @apiParam {String|Number} cityId=''  城市Id
 * @apiParam {String} kw=''  搜索关键字
 * 
 * @apiParamExample  {type} 请求示例:
 * {  
 *    cityId:10086,
 *    kw:"你好",
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} datas 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
     code:200
     data: {
            cinemas: [{
                        "cinemaId": 9127,
                        "name": "帕加尼激光影城（虹口店）",
                        "address": "虹口区四川北路1363号壹丰广场三楼",
                        "longitude": 121.49016459358052,
                        "latitude": 31.258215133589495,
                        "gpsAddress": "121.49016459358052:31.258215133589495",
                        "cityId": 310100,
                        "cityName": "上海市",
                        "districtId": 310109,
                        "districtName": "虹口区",
                        "district": {
                            "districtId": 310109,
                            "name": "虹口区"
                        },
                        "telephones": [
                            ""
                        ],
                        "logoUrl": "",
                        "businessTime": "",
                        "notice": "",
                        "isVisited": 0,
                        "lowPrice": 4400,
                        "Distance": 0.5836945632424313,
                        "eTicketFlag": 0,
                        "seatFlag": 1,
                        "ticketTypes": null
                    }],
            films: [{
                    filmId: 5828,
                    name: '东北虎',
                    poster: 'https://pic.maizuo.com/usr/movie/7c593107c5719afe9479d1c6a62d69fc.jpg',
                    actors: [ [Object], [Object], [Object], [Object], [Object] ],
                    director: '耿军',
                    category: '喜剧|剧情',
                    synopsis: '美玲即将临盆，她的丈夫徐东只好将家中心爱的狗拖朋友寄养到别处。马千里是债务缠身的建筑商，
                               他在穷途末路之时，偶然接收到了徐东的宠物。不幸的是，这只狗的死亡让两个站在人生岔路口的失意人狭路相逢，满
                               腔怒火的徐东在复仇的道路上屡战屡败，却意外发现了马千里不为人知的一面。与此同时，美玲也意外发现了徐东的秘
                               密......不易熄灭的仇恨之火被点燃，熄灭仇恨的灭火器迟迟没有打开。东北虎在动物园晒着太阳发着呆，它似乎已经
                               习惯了每天被饲养员扔下的食物喂养的生活。',
                    filmType: { name: '2D', value: 1 },
                    nation: '中国大陆',
                    language: '',
                    videoId: '',
                    premiereAt: 1642118400,
                    timeType: 3,
                    runtime: 118,
                    grade: '7.9',
                    item: { name: '2D', type: 1 },
                    isPresale: true,
                    isSale: false
                }]
     msg: "ok"
     filmstotal:100,
     cinamestotal:100
  },
}
 * 
 */
router.get('/search', (req, res) => {
    let { cityId, kw } = req.query
    console.log(cityId, kw);
    getNow()
    async function getNow() {
        let nowArr = await axios('https://m.maizuo.com/gateway', {
            params: {
                cityId,
                pageNum: 1,
                pageSize: 1000,
                type: 1
            },
            headers: {
                'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913"}',
                'X-Host': 'mall.film-ticket.film.list'
            }
        })
        nowArr = nowArr.data.data.films.map(item=>{
            return {...item,sell:'热映'}
        })
        let soonArr = await axios('https://m.maizuo.com/gateway', {
            params: {
                cityId,
                pageNum: 1,
                pageSize: 1000,
                type: 2
            },
            headers: {
                'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913"}',
                'X-Host': 'mall.film-ticket.film.list'
            }
        })
        soonArr = soonArr.data.data.films.map(item=>{
            return {...item,sell:'待映'}
        })
        let cinemaArr = await axios('https://m.maizuo.com/gateway', {
            params: {
                cityId,
                ticketFlag: 0,
            },
            headers: {
                'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913"}',
                'X-Host': 'mall.film-ticket.cinema.list'
            }
        })
        cinemaArr = cinemaArr.data.data.cinemas
        //最后影片信息数组
        let filmArr = [...nowArr,...soonArr]
        let newFilmArr = filmArr.filter(item=>{
            return item.name.includes(kw)
        })
        let newCinemaArr = cinemaArr.filter(item=>{
            return item.name.includes(kw)||item.address.includes(kw)||item.cityName.includes(kw)
        })
        let flen = filmArr.length
        let clen = cinemaArr.length
        // console.log(newFilmArr);
        res.send({code:200,filmstotal:flen,cinemastotal:clen,films:newFilmArr,cinemas:newCinemaArr,msg:'ok'})
    }

})


module.exports = router